package com.vonchenchen.myslidingtabpager.ui;

import android.content.Context;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vonchenchen.myslidingtabpager.MyApplication;
import com.vonchenchen.myslidingtabpager.R;
import com.vonchenchen.myslidingtabpager.holder.BaseWidgetHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vonchenchen on 2015/11/19 0019.
 */
public class MySlidingTabPager extends RelativeLayout{

    private Context mCtx;
    /** Tab布局 */
    private LinearLayout mTabsLayout;
    /** ViewPager */
    private ViewPager mContentViewPager;

    private List<TextView> mTabList;
    private List<BaseWidgetHolder> mHolderList;

    private MyPagerAdapter mMyPagerAdapter;

    private int mRecordPosition = -1;

    public MySlidingTabPager(Context context) {
        super(context);
        this.mCtx = context;
    }

    public MySlidingTabPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mCtx = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View.inflate(mCtx, R.layout.layout_slidingtabpager, this);
        mTabsLayout = (LinearLayout) findViewById(R.id.ll_tabs);
        mContentViewPager = (ViewPager) findViewById(R.id.vp_content);

        mTabList = new ArrayList<TextView>();
        mHolderList = new ArrayList<BaseWidgetHolder>();

        //监听ViewPager滚动
        mContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //ViewPager滑动 对应Tab选择
                updateTab(position, false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 添加Tab
     * @param tab
     */
    public void addTab(TextView tab){
        mTabList.add(tab);
    }

    /**
     * 添加holder
     * @param holder
     */
    public void addHolder(BaseWidgetHolder holder){
        mHolderList.add(holder);
    }

    /**
     * 开启显示
     */
    public void start(){
        initTabView();
        initViewPager();
    }

    private void initTabView(){
        //设置Layout参数，用于添加新的Tab
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, dip2px(MyApplication.getContext(), 40));
        params.weight = 1;

        for(int i=0; i<mTabList.size(); i++){

            TextView view = mTabList.get(i);
            view.setGravity(Gravity.CENTER);

            final int position = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    mContentViewPager.setCurrentItem(position);
                    updateTab(position, true);
                }
            });
            //将TextView加入到LinearLayout上
            mTabsLayout.addView(view, params);
            setViewUnSelected(view, i);
        }

        //初始化默认Tab颜色及其位置
        mRecordPosition = 0;
        setViewSelectd(mTabList.get(0), 0);
    }

    private void updateTab(int position, boolean isTouchTab){
        if(position != mRecordPosition){
            setViewSelectd(mTabList.get(position), position);
        }

        if(mRecordPosition >= 0 && !isTouchTab){
            setViewUnSelected(mTabList.get(mRecordPosition), mRecordPosition);
        }

        mRecordPosition = position;
    }

    /**
     * 当Pager被选择时设置对应Tab的颜色
     * @param view
     * @param position
     */
    private void setViewSelectd(View view, int position){
        if(position == 0){
            view.setBackgroundResource(R.drawable.shape_detail_rb_left_blue);
        }else if(position == mTabList.size()-1){
            view.setBackgroundResource(R.drawable.shape_detail_rb_right_blue);
        }else{
            view.setBackgroundResource(R.drawable.shape_detail_rb_mid_blue);
        }
    }

    /**
     * 取消Tab的选中颜色
     * @param view
     * @param position
     */
    private void setViewUnSelected(View view, int position){
        if(position == 0) {
            view.setBackgroundResource(R.drawable.shape_detail_rb_left_white);
        }else if(position == mTabList.size()-1){
            view.setBackgroundResource(R.drawable.shape_detail_rb_right_white);
        }else{
            view.setBackgroundResource(R.drawable.shape_detail_rb_mid_white);
        }
    }

    private void initViewPager(){
        mMyPagerAdapter = new MyPagerAdapter();
        mContentViewPager.setAdapter(mMyPagerAdapter);
    }

    public class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mTabList.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mHolderList.get(position).getRootView());
            return mHolderList.get(position).getRootView();
        }
    }

    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale);
    }
}
